package com.yan.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.jfinal.core.Controller;
import com.jfinal.json.FastJson;
import com.jfinal.kit.HttpKit;
import com.yan.bean.Blog;

import jodd.http.HttpRequest;
import jodd.http.HttpResponse;

public class IndexController extends Controller {

	public void index() {
		render("index.jsp");
	}

	// http://blog.csdn.net/u012374381/article/details/64440707
	// JFinal接收List 和单个参数
	public void indexParams() {
		Map<String, String> map = new HashMap<String, String>();
		String jsonString = HttpKit.readData(getRequest());
		Blog blog = FastJson.getJson().parse(jsonString, Blog.class);

		System.out.println("jsonString===" + jsonString + " blog====" + blog);
		System.out.println(getAttr("id") + " " + getAttr("title") + " getParaId=" + getPara("id") + " getParatitle="
				+ getPara("title") + "   getBean=" + getBean(Blog.class) + " getModel=" + getModel(Blog.class)
				+ " list====" + getParaValues("list[]") + "  bean=" + getBean(Blog.class, "") + " model="
				+ getModel(Blog.class));

		Map<String, String[]> maps = getParaMap();
		for (Entry<String, String[]> entry : maps.entrySet()) {
			System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
			for (String str : entry.getValue()) {
				System.out.println("key=" + entry.getKey() + "  value=" + str);
			}
		}

		String[] userids = this.getParaValues("list[]");
		for (String str : userids) {
			System.out.println(str);
		}
		map.put("name", "yan");
		renderJson(map);
	}

	public void indexJson() {
		Map<String, String> map = new HashMap<String, String>();
		String jsonString = HttpKit.readData(getRequest());
		Blog blog1 = FastJson.getJson().parse(jsonString, Blog.class);
		// ajax JSON.stringify(jsonData) 之后才算是json 数据,否则就是key=value格式
		Blog blog2 = FastJson.getJson().parse(getPara("jsonData"), Blog.class);
		System.out.println("jsonString===" + jsonString + " blog====" + blog1 + " " + blog2);
		map.put("name", "yan");
		map.put("sex", "男");
		renderJson(map);
	}

	public void test() {
		String url = "http://192.168.70.213:8080/chatServer/chat/sendPChatMessage/android/football?lang=zh&timeZone=8&message=&chatType=football&toUserId=null&sourceName=android&msgCode=5&msgId=e54ea308-d389-4824-9ebf-5262f04d577b&thirdId=3482618&deviceId=357095063448756&userId=HHLY00000004&phoneNum=13570185327&userLogo=http://tp.13322.com/img/2017-06-06/9212f265-8f28-40e3-9032-a2cb412e633c.jpg&userNick=诸葛看盘";
		// String url="http://localhost:10088/JFinal/a.html";
		HttpRequest httpRequest = HttpRequest.get(url).header("Accept-Encoding", "gzip");
		HttpResponse response = httpRequest.send().accept("utf-8");
		
	}
}
